package com.auto.learning.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.View;

/**
 * 图片工具类
 * 
 * @ClassName: ImageUtil All rights Reserved, Designed By isoftStone Copyright:
 *             Copyright(C) 2013 Company: isoftStone Holdings Limited
 * @author: Wen Weiquan
 * @version: V1.0
 * @date 2014-1-10 下午2:59:34
 */
public class ImageUtil {

	public static Bitmap decodeSampledBitmapFromFileWith(String fileName,
			int reqWidth, int reqHeight) {
		return decodeSampledBitmapFromFile(fileName, reqWidth, reqHeight);
	}

	/**
	 * 获取图片
	 * 
	 * @param viewId
	 * @param path
	 * @return
	 */
	public static Bitmap getScaleImage(View view, String fileName) {
		int width = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int height = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		view.measure(width, height);
		height = view.getMeasuredHeight();
		width = view.getMeasuredWidth();

		return decodeSampledBitmapFromFile(fileName, width, height);
	}

	public static Bitmap decodeSampledBitmapFromFile(String fileName,
			int reqWidth, int reqHeight) {

		Bitmap bitmap = null;

		try {
			File file = new File(fileName);
			if (file.exists()) {

				// 第一次解析将inJustDecodeBounds设置为true，来获取图片大小
				final BitmapFactory.Options options = new BitmapFactory.Options();
				options.inJustDecodeBounds = true;
				BitmapFactory.decodeFile(fileName, options);
				// 调用上面定义的方法计算inSampleSize值
				options.inSampleSize = calculateInSampleSize(options, reqWidth,
						reqHeight);
				// 使用获取到的inSampleSize值再次解析图片
				options.inJustDecodeBounds = false;

				bitmap = BitmapFactory.decodeFile(fileName, options);
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return bitmap;

	}

	public static Bitmap decodeSampledBitmapFromResource(Resources res,
			int resId, int reqWidth, int reqHeight) {
		// 第一次解析将inJustDecodeBounds设置为true，来获取图片大小
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(res, resId, options);
		// 调用上面定义的方法计算inSampleSize值
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);
		// 使用获取到的inSampleSize值再次解析图片
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeResource(res, resId, options);
	}

	/**
	 * 计算缩放比
	 * 
	 * @param options
	 * @param reqWidth
	 * @param reqHeight
	 * @return
	 */
	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// 源图片的高度和宽度
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;
		if (height > reqHeight || width > reqWidth) {
			// 计算出实际宽高和目标宽高的比率
			final int heightRatio = Math.round((float) height
					/ (float) reqHeight);
			final int widthRatio = Math.round((float) width / (float) reqWidth);
			// 选择宽和高中最小的比率作为inSampleSize的值，这样可以保证最终图片的宽和高
			// 一定都会大于等于目标的宽和高。
			inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
		}
		return inSampleSize;
	}
	
	public static Bitmap decodeSampledBitmap(File file,int reqWidth, int reqHeight) {
		// 第一次解析将inJustDecodeBounds设置为true，来获取图片大小
		return decodeSampledBitmap(file.getAbsoluteFile(),reqWidth,reqHeight);
	}
	
	public static Bitmap decodeSampledBitmap(String fileName,int reqWidth, int reqHeight) {
		// 第一次解析将inJustDecodeBounds设置为true，来获取图片大小
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;

		BitmapFactory.decodeFile(fileName,options);
		// 调用上面定义的方法计算inSampleSize值
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);
		// 使用获取到的inSampleSize值再次解析图片
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(fileName, options);
	}

	// decode这个图片并且按比例缩放以减少内存消耗，虚拟机对每张图片的缓存大小也是有限制的
	public static Bitmap decodeFile(File f, boolean compressFlag) {
		try {
			BitmapFactory.Options bitmapOptions = null;

			if (compressFlag) {
				// 压缩处理
				BitmapFactory.Options option = new BitmapFactory.Options();
				option.inJustDecodeBounds = true;
				BitmapFactory
						.decodeStream(new FileInputStream(f), null, option);

				// Find the correct scale value. It should be the power of 2.
				final int REQUIRED_SIZE = 70;
				int width_tmp = option.outWidth, height_tmp = option.outHeight;
				int scale = 1;
				while (true) {
					if (width_tmp / 2 < REQUIRED_SIZE
							|| height_tmp / 2 < REQUIRED_SIZE)
						break;
					width_tmp /= 2;
					height_tmp /= 2;
					scale *= 2;
				}

				// decode with inSampleSize
				BitmapFactory.Options option2 = new BitmapFactory.Options();
				option2.inSampleSize = scale;

				bitmapOptions = option2;

			} else {
				// 不压缩处理
				bitmapOptions = new BitmapFactory.Options();
			}

			return BitmapFactory.decodeStream(new FileInputStream(f), null,
					bitmapOptions);

		} catch (FileNotFoundException e) {
		}
		return null;
	}
	
	
	
	public static Drawable getDrawble(String fileName){
		File file=new File(fileName);
		if(!file.exists()){
			return null;
		}
		return Drawable.createFromPath(fileName);

	}
}
